import json


class JsonPlaceholderReplacer:
    def __init__(self, json_data):
        # 将 JSON 字符串解析为 Python 字典
        self.json_data = json_data

    def replace_placeholder(self, placeholder, value):
        """递归地在 JSON 中替换占位符"""
        self._replace_recursive(self.json_data, placeholder, value)

    def _replace_recursive(self, data, placeholder, value):
        """递归地遍历和替换 JSON 数据中的占位符"""
        if isinstance(data, dict):
            for key, val in data.items():
                if isinstance(val, (dict, list)):
                    self._replace_recursive(val, placeholder, value)
                elif isinstance(val, str) and placeholder in val:
                    data[key] = val.replace(placeholder, value)
                elif key == placeholder:
                    data[key] = value  # 直接替换为对象
        elif isinstance(data, list):
            for index, item in enumerate(data):
                if isinstance(item, (dict, list)):
                    self._replace_recursive(item, placeholder, value)
                elif isinstance(item, str) and placeholder in item:
                    data[index] = item.replace(placeholder, value)

    def get_modified_json(self):
        """返回替换后的 JSON 字符串"""
        return json.dumps(self.json_data, ensure_ascii=False, indent=4)
